%%
%% Share the load of the common configuration with an aim to shift a maximum CPU DeltaCPU.
%% 
%% Parameters:
%% 	PLANLC: Common configuration plan parts for the node Large
%%  PLANSC: Common parts for the node Small
%%  DeltaCPU: CPU load to shift
%%  
%%  Parameter formats: PLAN = [{CID, CPU, MEM}]
%%
share_common(PLANLC, PLANSC, DeltaCPU)->
	ListS = [{CID, {CPU, MEM}} || {CID, CPU, MEM} <- PLANSC],
	DictS = dict:from_list(ListS),
	share_common(PLANLC, DictS, DeltaCPU, 1).

%%
%% Share the load of the common configuration with an aim to shift a maximum CPU DeltaCPU.
%% Currently try to shift Component with Index in PLANLC
%% 
%% Parameters:
%% 	PLANLC: Common configuration plan parts for the node Large
%%  DictS: CID to {CPU, MEM} dictionary for the node Small
%%  DeltaCPU: CPU load to shift
%%  Index: Current scanning index for the component to shift
%%  
%%  Parameter formats: PLAN = [{CID, CPU, MEM}]
%%
share_common(PLANLC, DictS, DeltaCPU, Index) ->
	if
		Index>length(PLANLC)->
			PLANSC = dict_to_list(DictS),
			{[], PLANSC, DeltaCPU};
		true->
			CurrentItem = lists:nth(Index, PLANLC),
			{CID, CPU, MEM} = CurrentItem,
			{TakenCPU, TakenMEM} = dict:fetch(CID, DictS),
			if
				DeltaCPU > CPU->
					DictSNew = dict:store(CID, {TakenCPU + CPU, TakenMEM}, DictS),
					share_common(PLANLC, DictSNew, DeltaCPU - CPU, Index + 1);
				true->
					DictSNew = dict:store(CID, {TakenCPU + DeltaCPU, TakenMEM}, DictS),
					PLANLC1 = lists:sublist(PLANLC, Index+1, length(PLANLC) - Index),
					if
						CPU - DeltaCPU > 0->
							PLANLCNew = lists:append(PLANLC1, [{CID, CPU - DeltaCPU, MEM}]);
						true ->
							PLANLCNew = PLANLC1
					end,
					PLANSC = dict_to_list(DictSNew),
					{PLANLCNew, PLANSC, 0}
			end
	end.